/**************************************************************************************************

	NSurvey - The web survey and form engine
	Copyright (c) 2004, Thomas Zumbrunn. (http://www.nsurvey.org)

	Dotnetnuke PA module 
	Copyright (c) 2004, Oliver Hine (oliver.hine@gmail.com)

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

************************************************************************************************/
using DotNetNuke;
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Votations.NSurvey.DataAccess;
using Votations.NSurvey.Data;
using Votations.NSurvey.BusinessRules;
using Votations.NSurvey.WebAdmin.UserControls;
using Votations.NSurvey.Web;

namespace Votations.NSurvey.WebAdmin
{
	public class LibraryDirectory : PageBase
	{
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.LibraryDataList.ItemCreated += new System.Web.UI.WebControls.DataListItemEventHandler(this.LibraryDataList_ItemCreated);
			this.LibraryDataList.ItemCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.LibraryDataList_ItemCommand);
			this.AddNewLibraryButton.Click += new System.EventHandler(this.AddNewLibraryButton_Click);
			this.AddLibraryButton.Click += new System.EventHandler(this.AddLibraryButton_Click);
			this.UpdateLibraryButton.Click += new System.EventHandler(this.UpdateLibraryButton_Click);
			this.DeleteLibraryButton.Click += new System.EventHandler(this.DeleteLibraryButton_Click);
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion

		protected System.Web.UI.WebControls.Label MessageLabel;
		protected HeaderControl Header;
		protected System.Web.UI.WebControls.Literal AddLibraryTitle;
		protected System.Web.UI.WebControls.Literal LibraryNameLabel;
		protected System.Web.UI.WebControls.TextBox LibraryNameTextBox;
		protected System.Web.UI.WebControls.Button AddNewLibraryButton;
		protected System.Web.UI.WebControls.Button AddLibraryButton;
		protected System.Web.UI.WebControls.Button UpdateLibraryButton;
		protected System.Web.UI.WebControls.Button DeleteLibraryButton;
		protected System.Web.UI.WebControls.DataList LibraryDataList;
		protected System.Web.UI.WebControls.PlaceHolder editplaceholder;
		protected System.Web.UI.WebControls.Literal LibraryTitle;


		private void Page_Load(object sender, System.EventArgs e)
		{
			try
			{
				SetupSecurity();
				LocalizePage();
				MessageLabel.Visible = false;

				if (!Page.IsPostBack)
				{
					Header.SurveyId = SurveyId;
					BindData();
				}

				DeleteLibraryButton.Attributes.Add("onClick",
					"javascript:if(confirm('" +GetPageResource("DeleteLibraryConfirmationMessage")+ "')== false) return false;");
			}
			catch( Exception exc)
			{
				DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this,exc);
			}
		}

		private void LocalizePage()
		{
			LibraryTitle.Text = GetPageResource("LibraryTitle");
			AddNewLibraryButton.Text = GetPageResource("AddNewLibraryButton");
			DeleteLibraryButton.Text = GetPageResource("DeleteLibraryButton");
			UpdateLibraryButton.Text = GetPageResource("UpdateLibraryButton");
			LibraryNameLabel.Text = GetPageResource("LibraryNameLabel");
		}

		private void SetupSecurity()
		{
			CheckRight(NSurveyRights.AccessLibrary, true);
			AddNewLibraryButton.Visible = NSurveyUser.Identity.IsAdmin || CheckRight(NSurveyRights.ManageLibrary, false);
		}

		/// <summary>
		/// Get the current DB stats and fill 
		/// the label with them
		/// </summary>
		private void BindData()
		{
			// Retrieve the library data
			LibraryData libraryData = 
				new Libraries().GetLibraries();
			LibraryDataList.DataSource = libraryData;
			LibraryDataList.DataMember = "Libraries";
			LibraryDataList.DataBind();
		}

		private void AddLibraryButton_Click(object sender, System.EventArgs e)
		{
			if (LibraryNameTextBox.Text.Length == 0)
			{
				MessageLabel.Text = GetPageResource("MissingLibraryNameMessage");
				MessageLabel.Visible = true;
				return;
			}
			else
			{
				LibraryData libraryData = new LibraryData();
				LibraryData.LibrariesRow library = libraryData.Libraries.NewLibrariesRow();
				library.LibraryName = LibraryNameTextBox.Text;
				libraryData.Libraries.Rows.Add(library);
				new Library().AddLibrary(libraryData);
				LibraryNameTextBox.Text = string.Empty;
				BindData();
				MessageLabel.Text = GetPageResource("AddedLibraryNameMessage");
				MessageLabel.Visible = true;
			}
		}

		private void LibraryDataList_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
		{
			LibraryData library = new Libraries().GetLibraryById(int.Parse(e.CommandArgument.ToString()));
			LibraryNameTextBox.Text = library.Libraries[0].LibraryName;
			ViewState["LibraryId"] = library.Libraries[0].LibraryId;
			AddLibraryButton.Visible = false;
			AddLibraryTitle.Text = GetPageResource("UpdateLibraryTitle");
			UpdateLibraryButton.Visible = true;
			DeleteLibraryButton.Visible = true;
			editplaceholder.Visible = true;
		}

		private void UpdateLibraryButton_Click(object sender, System.EventArgs e)
		{
			if (LibraryNameTextBox.Text.Length == 0)
			{
				MessageLabel.Text = GetPageResource("MissingLibraryNameMessage");
				MessageLabel.Visible = true;
				editplaceholder.Visible = true;
				return;
			}
			else
			{
				LibraryData libraryData = new LibraryData();
				LibraryData.LibrariesRow library = libraryData.Libraries.NewLibrariesRow();
				library.LibraryId = int.Parse(ViewState["LibraryId"].ToString());
				library.LibraryName = LibraryNameTextBox.Text;
				libraryData.Libraries.Rows.Add(library);
				new Library().UpdateLibrary(libraryData);
				LibraryNameTextBox.Text = string.Empty;
				BindData();
				MessageLabel.Text = GetPageResource("UpdatedLibraryNameMessage");
				MessageLabel.Visible = true;
				editplaceholder.Visible = false;
			}
		}

		private void DeleteLibraryButton_Click(object sender, System.EventArgs e)
		{
			new Library().DeleteLibrary(int.Parse(ViewState["LibraryId"].ToString()));
			BindData();
			MessageLabel.Text = GetPageResource("DeletedLibraryNameMessage");
			MessageLabel.Visible = true;
			editplaceholder.Visible = false;
			LibraryNameTextBox.Text = string.Empty;
		}

		private void AddNewLibraryButton_Click(object sender, System.EventArgs e)
		{
			UpdateLibraryButton.Visible = false;
			DeleteLibraryButton.Visible = false;
			AddLibraryButton.Visible = true;
			AddLibraryTitle.Text = GetPageResource("AddLibraryTitle");
			editplaceholder.Visible = true;
		}

		private void LibraryDataList_ItemCreated(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
		{
			e.Item.FindControl("EditDirectoryHyperLink").Visible = 
				NSurveyUser.Identity.IsAdmin || CheckRight(NSurveyRights.ManageLibrary, false);
		}

	}
}
